//
//  WGQChangePasswordViewController.m
//  meiang_edu
//
//  Created by gq w on 2018/10/12.
//  Copyright © 2018 com.meiang.meiang_edu. All rights reserved.
//  修改密码

#import "WGQChangePasswordViewController.h"

@interface WGQChangePasswordViewController ()

@property (nonatomic,strong) UITextField * oldPassword;//旧密码
@property (nonatomic,strong) UITextField * nnewPassword;//新密码
@property (nonatomic,strong) UITextField * surePassword;//确认密码
@property (nonatomic,strong) UIButton * rightButton;//右上角的按钮

@end

@implementation WGQChangePasswordViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.edgesForExtendedLayout = UIRectEdgeBottom;
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.extendedLayoutIncludesOpaqueBars = NO;
    self.view.backgroundColor = FFColor(0xebebeb);
    self.title = @"修改密码";
    [self drawRightButton];
    [self addSubViews];
}

- (void)rightButtonActionWithSender:(UIButton *)sender
{
    if (self.oldPassword.text.length == 0)
    {
        [MBManager showBriefAlert:@"请输入原密码"];
        return;
    }
    
    if (self.nnewPassword.text.length == 0)
    {
        [MBManager showBriefAlert:@"请输入新密码"];
        return;
    }
    
    if (self.surePassword.text.length == 0)
    {
        [MBManager showBriefAlert:@"请确认密码"];
        return;
    }
    
    
    if ([self.nnewPassword.text isEqualToString:self.surePassword.text])
    {
        //两次新密码输入一致
        MAUserInfo *userinfo = [MAUserInfoTool userinfo];
        [MBManager showLoading];
        NSDictionary *body = @{@"oldpwd" : self.oldPassword.text, @"newpwd" : self.surePassword.text, @"us_id" : userinfo.ID};
        NSError *error;
        NSData *jsonData = [NSJSONSerialization dataWithJSONObject:body options:0 error:&error];
        NSString *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
        AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
        NSMutableURLRequest *req = [[AFJSONRequestSerializer serializer] requestWithMethod:@"POST" URLString:CHANGE_PASSWORD parameters:nil error:nil];
        [req setValue:@"application/text" forHTTPHeaderField:@"Content-Type"];
        [req setValue:@"application/json" forHTTPHeaderField:@"Accept"];
        [req setHTTPBody:[jsonString dataUsingEncoding:NSUTF8StringEncoding]];
        [[manager dataTaskWithRequest:req completionHandler:^(NSURLResponse * _Nonnull response, id  _Nullable responseObject, NSError * _Nullable error)
        {
            [MBManager hideAlert];
            if (!error)
            {
                NSInteger msgcode = [responseObject[@"msgcode"] integerValue];
                NSString *message = responseObject[@"message"];
                if (msgcode == 1)
                {
                    [MBManager showBriefAlert:message];
                    [self.navigationController popViewControllerAnimated:YES];
                } else
                {
                    [MBManager showBriefAlert:message];
                }
                DLog(@"Reply JSON: %@", responseObject);
            } else
            {
                DLog(@"Error: %@, %@, %@", error, response, responseObject);
                [MBManager showBriefAlert:@"网络错误"];
            }
        }] resume];
    }
    else
    {
        //两次新密码输入不一致
        [MBManager showBriefAlert:@"两次新密码输入不一致"];
    }
}

//绘制右上角的按钮
- (void)drawRightButton
{
    self.rightButton = [UIButton buttonWithType:UIButtonTypeCustom];
    self.rightButton.frame = CGRectMake(0, 0, 16 * kScale, 16 * kScale);
    [self.rightButton setBackgroundImage:[UIImage imageNamed:@"savebutton_logo"] forState:UIControlStateNormal];
    [self.rightButton addTarget:self action:@selector(rightButtonActionWithSender:) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:self.rightButton];
}

- (void)addSubViews
{
    UILabel * oldPasswordLabel = [[UILabel alloc] initWithFrame:CGRectMake(0 *kScale, 0 *kScale, 50 *kScale, 44 *kScale)];
    oldPasswordLabel.textColor = FFColor(0x666666);
    oldPasswordLabel.text = @"  原密码";
    oldPasswordLabel.font = [UIFont systemFontOfSize:12 *kScale];
    oldPasswordLabel.textAlignment = NSTextAlignmentLeft;
    oldPasswordLabel.textColor = FFColor(0x666666);
    self.oldPassword = [[UITextField alloc] initWithFrame:CGRectMake(12 * kScale, 12 * kScale, kScreenWidth - 24 * kScale, 44 * kScale)];
    self.oldPassword.backgroundColor = [UIColor whiteColor];
    self.oldPassword.secureTextEntry = YES;
    self.oldPassword.leftView = oldPasswordLabel;
    self.oldPassword.leftViewMode = UITextFieldViewModeAlways;
    self.oldPassword.borderStyle = UITextBorderStyleNone;
    self.oldPassword.layer.masksToBounds = YES;
    self.oldPassword.layer.cornerRadius = 7 * kScale;
    self.oldPassword.font = [UIFont fontWithName:kMainFontFormat size:12 * kScale];
    [self.view addSubview:self.oldPassword];
    
    
    UILabel * nnewPasswordLabel = [[UILabel alloc] initWithFrame:CGRectMake(0 *kScale, 0 *kScale, 50 *kScale, 44 *kScale)];
    nnewPasswordLabel.text = @"  新密码";
    nnewPasswordLabel.font = [UIFont systemFontOfSize:12 *kScale];
    nnewPasswordLabel.textAlignment = NSTextAlignmentLeft;
    nnewPasswordLabel.textColor = FFColor(0x666666);
    self.nnewPassword = [[UITextField alloc] init];
    self.nnewPassword.secureTextEntry = YES;
    self.nnewPassword.backgroundColor = [UIColor whiteColor];
    self.nnewPassword.leftView = nnewPasswordLabel;
    self.nnewPassword.leftViewMode = UITextFieldViewModeAlways;
    self.nnewPassword.borderStyle = UITextBorderStyleNone;
    self.nnewPassword.layer.masksToBounds = YES;
    self.nnewPassword.layer.cornerRadius = 7 * kScale;
    self.nnewPassword.font = [UIFont fontWithName:kMainFontFormat size:12 * kScale];
    [self.view addSubview:self.nnewPassword];
    [self.nnewPassword mas_makeConstraints:^(MASConstraintMaker *make)
    {
        make.left.equalTo(self.view.mas_left).offset(12 * kScale);
        make.right.equalTo(self.view.mas_right).offset(-12 * kScale);
        make.top.equalTo(self.oldPassword.mas_bottom).offset(4 * kScale);
        make.height.mas_equalTo(44 * kScale);//还不确定
    }];
    
    UILabel * surePasswordLabel = [[UILabel alloc] initWithFrame:CGRectMake(0 *kScale, 0 *kScale, 70 *kScale, 44 *kScale)];
    surePasswordLabel.text = @"  确认密码";
    surePasswordLabel.font = [UIFont systemFontOfSize:12 *kScale];
    surePasswordLabel.textAlignment = NSTextAlignmentLeft;
    surePasswordLabel.textColor = FFColor(0x666666);
    self.surePassword = [[UITextField alloc] init];
    self.surePassword.secureTextEntry = YES;
    self.surePassword.backgroundColor = [UIColor whiteColor];
    self.surePassword.leftView = surePasswordLabel;
    self.surePassword.leftViewMode = UITextFieldViewModeAlways;
    self.surePassword.borderStyle = UITextBorderStyleNone;
    self.surePassword.layer.masksToBounds = YES;
    self.surePassword.layer.cornerRadius = 7 * kScale;
    self.surePassword.font = [UIFont fontWithName:kMainFontFormat size:12 * kScale];
    [self.view addSubview:self.surePassword];
    [self.surePassword mas_makeConstraints:^(MASConstraintMaker *make)
    {
        make.left.equalTo(self.view.mas_left).offset(12 * kScale);
        make.right.equalTo(self.view.mas_right).offset(-12 * kScale);
        make.top.equalTo(self.nnewPassword.mas_bottom).offset(4 * kScale);
        make.height.mas_equalTo(44 * kScale);//还不确定
    }];
}

@end
